<template>
	<xn-form-container
		:title="formData.id ? '编辑订单' : '增加订单'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="用户名称：" name="user_id">
				<a-select
					v-model:value="formData.user_id"
					show-search
					placeholder="请选择用户"
					:options="userOptions"
					allow-clear
					option-filter-prop="label"
				/>
			</a-form-item>
			<a-form-item label="姓名：">
				<a-input v-model:value="formData.name" readonly></a-input>
			</a-form-item>
			<a-form-item label="电话：">
				<a-input v-model:value="formData.phone" phone></a-input>
			</a-form-item>

			<a-form-item label="公司名称：">
				<a-input v-model:value="formData.company_name" readonly></a-input>
			</a-form-item>
			<a-form-item label="邮箱：">
				<a-input v-model:value="formData.email" readonly></a-input>
			</a-form-item>
			<a-form-item label="咨询业务：">
				<a-input v-model:value="formData.business_name" readonly></a-input>
			</a-form-item>
			<a-form-item label="其他内容：">
				<a-input v-model:value="formData.remark" readonly></a-input>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	import userApi from '../../sys/user/api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		time: []
	})

	const columns = [
		{
			title: '用户名称',
			dataIndex: 'username',
			width: '200px',
			fixed: 'left',
			ellipsis: { showTitle: true }
		},
		{
			title: '服务模块',
			dataIndex: 'name',
			width: 200
		},
		{
			title: '开始时间',
			dataIndex: 'start_time',
			width: 200
		},
		{
			title: '结束时间',
			dataIndex: 'end_time',
			width: 200
		},
		{
			title: '状态',
			dataIndex: 'status',
			width: 100
		},
		{
			title: '创建时间',
			dataIndex: 'create_time',
			width: 200
		},
		{
			title: '修改时间',
			dataIndex: 'modified_time',
			width: 200
		},
		{
			title: '操作',
			dataIndex: 'action',
			fixed: 'right',
			align: 'center',
			width: '200px'
		}
	]
	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		index = id
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.value.time = [dayjs(record.start_time), dayjs(record.end_time)]
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	const userOptions = ref([])

	const loadUserList = () => {
		userApi.modulePage({ pageNow: 1, pagesize: 10000 }).then((res) => {
			userOptions.value = (res.record || []).map((item) => {
				return {
					label: item.username,
					value: item.id
				}
			})
		})
	}
	loadUserList()

	// 默认要校验的
	const formRules = {
		user_id: [required('请输入客户名称')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				let user_name = userOptions.value.find((item) => {
					return item.value == formData.value.user_id
				}).label
				api
					.submitForm(
						{
							...formData.value,
							user_name,
							profile_id: formData.value.id
						},
						formData.value.id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
